<?php
namespace app\index\controller;
use app\model\Category as CategoryModel;

use think\Db;


class Category extends Base
{   
    protected $noCheckToken = ['index','read'];
    
    /**
     * @Author   CCH
     * @DateTime 2020-05-23T12:10:10+0800
     * @return   数据列表
     */
    public function index(){
        $map_params = [
            
        ];
        $map = getSearchParam($map_params);
        $map[] = ['pid','=',0];

        $model = new CategoryModel();
        $list = $model->where($map)->order('id','asc')->select();
        $result = [];


        foreach ($list as $k => $vo) {
            $childs = $model->where('pid',$vo['id'])->select()->toarray();
            if (empty($childs)) {
                $childs = [];
            }else{
                foreach ($childs as $c_k => $child) {
                    $childs[$c_k]['product_num'] = Db::name('product')->where('category_id',$child['id'])->count();
                }
            }

            $list[$k]['childs'] = $childs;
        }
        
        return apiResult('2000','获取成功',$list);
    }




}
